var App = function() {
    
	var upload,upload_mulit;
    return {
        /**
         * 依赖组件
         */
        requires : ['Fw.Upload'],
        /**
         * 初始化
         * 应用入口
         */
        init : function(require) {
            this.initUpload();
            this.initEvents();
        },
        /**
         * 
         */
        initUpload: function(){
        	upload = new Fw.Upload({
        		contentEl: 'file',//div ID值
        		//fileType: 'png,jpg', //支持上传文件类型,为空时不限制文件类型，（文件类型以,分隔。如'jpg,png,doc' 或 '.jpg,.png,.doc'）
        		mulit: false // 多文件上传 ，默认true
        	});
        	upload_mulit = new Fw.Upload({
        		contentEl: 'file-mulit',//div ID值
        		//fileType: 'png,jpg', //支持上传文件类型,为空时不限制文件类型，（文件类型以,分隔。如'jpg,png,doc' 或 '.jpg,.png,.doc'）
        		mulit: true // 多文件上传 ，默认true
        	});
        },
        /**
         * 初始化事件
         */
        initEvents : function() {
            //点击登录按钮时触发
            $('#save').on('click', Fw.bind(this.onSave, this));
            //点击获取值按钮时触发
            $('#get').on('click', Fw.bind(this.getValue, this));
            // 回显值
            $('#set').on('click', Fw.bind(this.setValue, this));
            //点击清空按钮时触发
            $('#clear').on('click', Fw.bind(this.clearValues, this));
            //点击查看状态按钮时触发
            $('#view').on('click', Fw.bind(this.view, this));
            //点击编辑状态按钮时触发
            $('#edit').on('click', Fw.bind(this.edit, this));
            //点击允许下载按钮时触发
            $('#download').on('click', Fw.bind(this.download, this));
            //点击不允许下载按钮时触发
            $('#unDownload').on('click', Fw.bind(this.unDownload, this));
            //点击允许预览按钮时触发
            $('#preview').on('click', Fw.bind(this.preview, this));
            //点击不允许预览按钮时触发
            $('#unPreview').on('click', Fw.bind(this.unPreview, this));
        	//文件上传成功后
        	upload.on('success', this.onSuccess, this);
        	upload_mulit.on('success', this.onSuccess, this);
        	//文件上传失败后
        	upload.on('error', this.onFailure, this);
        	upload_mulit.on('error', this.onFailure, this);
        	//文件预览
        	upload.on('preview', this.onPreview, this);
        	upload_mulit.on('preview', this.onPreview, this);
        },
        /**
         * 文件上传成功后
         */
        onSuccess: function(u, data, result, values){
        	alert('success')
        	alert(Fw.encode(data))
        	alert(Fw.encode(values))
        },
        /**
         * 文件上传失败后
         */
        onFailure: function(status, textStatus, error){
        	alert('error')
        },
        /**
         * 点击预览
         */
        onPreview: function(u, fileId, url, fileType){
        	alert(fileId)
        	alert(url)
        	alert(fileType)
        },
        /**
         * 保存
         */
        onSave: function(){
//        	var obj = upload.getValue();
//        	Fw.util.Ajax.execute(obj,{
//        		service: 'demoFileInfoService',
//        		method: 'executeFile',
//        		success: function(data,result){
//        			alert(data)
//        			alert(result)
//        		}
//        	});
        	var me = this;
        	var files = upload_mulit.getValue();
        	Fw.util.Ajax.update(files,{
        		service: 'demo2Service',
        		method: 'executeFiles',
        		success: function(data,result){
        			Fw.Msg.info('保存成功', function(){
                        me.clearValues();
                    });
        		}
        	});
        },
        setValue: function(){
        	var o = {
			    "fileId" : "98D67723268B4C7EA06F5376F2A15886",
			    "fileName" : "CSDN",
			    "fileType" : ".png",
			    "fileSize" : 2697,
			    "operation" : "UPDATE",
			    "tmp" : false
		    };
        	var o1 = {
			    "fileId" : "5B03BB7A23054162A23DB395AAD3F212",
			    "fileName" : "QQ截图20150813095324",
			    "fileType" : ".png",
			    "fileSize" : 49556,
			    "operation" : "UPDATE",
			    "tmp" : false
		    };
        	upload.setValue(o);
        	upload_mulit.setValue([o,o1]);
        },
        /**
         * 清空上传文件信息
         */
        clearValues: function(){
	        upload.clearValue();
	        upload_mulit.clearValue();
        },
        /**
         * 获取已上传文件信息
         */
        getValue: function(){
        	alert('单文件上传：\n'+Fw.encode(upload.getValue()))
        	alert('多文件上传：\n'+Fw.encode(upload_mulit.getValue()))
        },
        view: function(){
        	upload_mulit.setView(true);
        	upload.setView(true);
        },
        edit: function(){
        	upload_mulit.setView(false);
        	upload.setView(false);
        },
        download: function(){
        	upload_mulit.setAllowDownload(true);
        	upload.setAllowDownload(true);
        },
        unDownload: function(){
       		upload_mulit.setAllowDownload(false);
       		upload.setAllowDownload(false);
       	},
       	preview: function(){
       		upload_mulit.setAllowPreview(true);
       		upload.setAllowPreview(true);
       	},
       	unPreview: function(){
       		upload_mulit.setAllowPreview(false);
       		upload.setAllowPreview(false);
       	},
        /**
         * 文件下载
         */
        fileDownload: function(){
        	var id = '5B03BB7A23054162A23DB395AAD3F212';
        	window.open(Fw.getBasePath() + '/channel/httpDownload.do?fileId='+id);
        }
    };
}();

Fw.onReady(App); 